/*{
 code:1,
 mess:"", //系统报错的信息 code =0

 }
 {
 code:1,
 mess:"", //系统报错的信息 code =0
 data:{
 业务
 }
 }
 */
(function(global, $) {
    function rest(type, url, data, callback) {
        if (typeof data === "function") {
            callback = data;
            data = {};
        }

        var der = $.Deferred();
        $.ajax({
            type: type,
            url: url,
            data: data,
            dataType:"json",
            // contentType: "application/json;charset=UTF-8",
            success: function(rs) {
                alert()
                if(rs.mess  === undefined)  rs.mess = null;
                if(rs.code === -1){

                    // window.location.href="/admin/login_page.html"
                    // der.resolveWith(this, [rs.mess]);
                }else if (rs.code === 1) {
                    if (typeof callback === 'function') {
                        callback.call(this, rs.mess, rs.data);
                    }
                    der.resolveWith(this, [rs.mess, rs.data]);
                } else {

                    der.rejectWith(this, [rs.mess]);
                }
            },
            error: function() {

                der.rejectWith(this);
            }
        });

        return der.promise();
    }

    $.extend({
        restGet: function() {
            return rest.apply(this, ['GET'].concat(Array.prototype.slice.call(arguments, 0)));
        },
        restPost: function() {
            return rest.apply(this, ['POST'].concat(Array.prototype.slice.call(arguments, 0)));
        }
    });


    //全局 弹出层提示报错 依赖layer 弹出层
    function BaseMessDialog(alert,mess ,time , endcallback) {
        layer.open({
            title: false,
            type: 1,
            closeBtn: 0, //不显示关闭按钮
            shift: 1,
            shadeClose: false, //开启遮罩关闭
            content: '<div class="alert clearmarg '+ alert +'">'+mess +'</div>',
            offset: '100px',
            time: time || 0,
            area: 'auto',
            maxWidth:"auto",
            end :endcallback
        });
}

    $.extend({

        rootErrorDialog:function ( mess,time,endcallback) {
            var rootError= mess || "网络延迟,请刷新";
            return BaseMessDialog("alert-danger",rootError,time,endcallback)
        },
        rootSuccessDialog:function ( mess,time,endcallback) {
            var rootSuccess= mess || "成功了";
            return BaseMessDialog("alert-success",rootSuccess,time,endcallback)
        },
        rootOpenDialog:function (opt) {

            var opt = $.extend({
                title: false,
                type: 1,
                skin: 'layui-layer-demo', //样式类名
                closeBtn: 1, //不显示关闭按钮
                shift: 0,
                offset: '180px',
                shadeClose: false, //开启遮罩关闭
                content: '...',
                area: '460px'
            },opt);
            return layer.open(opt);
        }
    })

    $.extend({
        getTime: function (){
        var currDate = new Date();
        var d = new Date();
        var YMDHMS = d.getFullYear() + "-" +(d.getMonth()+1) + "-" + d.getDate() + " " + d.getHours() + ":" + d.getMinutes() + ":" + d.getSeconds();
        return YMDHMS
        },
        printUrl:'http://114.55.107.209:8082/',
        getUrlLocation:function () {
            var hrefhash = window.location.hash;
            var hrefhashArr=hrefhash.split("/");

            return hrefhashArr[hrefhashArr.length-1];
        },
        getApplet:function getApplet(){
            if(document.applets.length > 0){
                return document.applets[0];
            }else{
                return "";
            }
        },
        funcUrl: function (name, value, type) {
            var loca = window.location;
            var baseUrl = type == undefined ? loca.origin + loca.pathname + "?" : "";
            var query = loca.search.substr(1);
            // 如果没有传参,就返回 search 值 不包含问号
            if (name == undefined) {
                return query
            }
            // 如果没有传值,就返回要查询的参数的值
            if (value == undefined) {
                var val = query.match(new RegExp("(^|&)" + name + "=([^&]*)(&|$)"));
                return val != null ? decodeURI(val[2]) : null;
            }
            ;
            var url;
            if (query == "") {
                // 如果没有 search 值,则返回追加了参数的 url
                url = baseUrl + name + "=" + value;
            } else {
                // 如果没有 search 值,则在其中修改对应的值,并且去重,最后返回 url
                var obj = {};
                var arr = query.split("&");
                for (var i = 0; i < arr.length; i++) {
                    arr[i] = arr[i].split("=");
                    obj[arr[i][0]] = arr[i][1];
                }
                ;
                obj[name] = value;
                url = baseUrl + JSON.stringify(obj).replace(/[\"\{\}]/g, "").replace(/\:/g, "=").replace(/\,/g, "&");
            }
            ;
            return url;
        }
    })


}) (window, jQuery);

Date.prototype.format = function(format) {
    var date = {
        "M+": this.getMonth() + 1,
        "d+": this.getDate(),
        "h+": this.getHours(),
        "m+": this.getMinutes(),
        "s+": this.getSeconds(),
        "q+": Math.floor((this.getMonth() + 3) / 3),
        "S+": this.getMilliseconds()
    };
    if (/(y+)/i.test(format)) {
        format = format.replace(RegExp.$1, (this.getFullYear() + '').substr(4 - RegExp.$1.length));
    }
    for (var k in date) {
        if (new RegExp("(" + k + ")").test(format)) {
            format = format.replace(RegExp.$1, RegExp.$1.length == 1
                ? date[k] : ("00" + date[k]).substr(("" + date[k]).length));
        }
    }
    return format;
}
